/**
 * jims
 */
package com.jims.master.modules.oper.service;

import com.jims.master.common.service.CrudService;
import com.jims.master.common.utils.ChineseCharToEnUtils;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.impl.BaseDto;
import com.jims.master.modules.oper.dao.ClinicItemDictDao;
import com.jims.master.modules.oper.entity.ClinicItemDict;
import com.jims.master.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 诊疗项目Service
 * @author 赵宁
 * @version 2017-02-23
 */
@Service
@Transactional(readOnly = true)
public class ClinicItemDictService extends CrudService<ClinicItemDictDao, ClinicItemDict> {
	@Autowired
	private ClinicItemDictDao clinicItemDictDao;

	@Transactional(readOnly = false)
	public int saveItem(ClinicItemDict clinicItemDict) {
		int num = 0;
		if(clinicItemDict!=null&& StringUtils.isNotBlank(clinicItemDict.getId())){
			if(StringUtils.isNotBlank(clinicItemDict.getItemName())){
				clinicItemDict.setInputCode(ChineseCharToEnUtils.getAllFirstLetter(clinicItemDict.getItemName()));
			}
			clinicItemDict.preUpdate();
			num = clinicItemDictDao.update(clinicItemDict);
		}else{
			if(StringUtils.isNotBlank(clinicItemDict.getItemName())){
				clinicItemDict.setInputCode(ChineseCharToEnUtils.getAllFirstLetter(clinicItemDict.getItemName()));
			}
			clinicItemDict.setOrgId(UserUtils.getOrgId());
			clinicItemDict.preInsert();
			getMaxItemCode(clinicItemDict);
			num = clinicItemDictDao.insert(clinicItemDict);
		}
		return num;
	}
	public void getMaxItemCode(ClinicItemDict clinicItemDict){
		int maxItemCode = clinicItemDictDao.getMaxItemCode(clinicItemDict.getOrgId(),clinicItemDict.getItemClass());
		String itemCode = "";
		if(maxItemCode == 0){
			itemCode = "I"+clinicItemDict.getItemClass()+"1";
		} else {
			itemCode = "I"+clinicItemDict.getItemClass() + (maxItemCode+1);
		}
		clinicItemDict.setItemCode(itemCode);
	}

	/**
	 * 根据诊疗项目查询所有对照
	 * @param clinicId
	 * @return
	 * @author CTQ
	 */
	public List<BaseDto> findVsChargeByClinicId(String clinicId){
		return clinicItemDictDao.findVsChargeByClinicId(clinicId);
	}

    /**
     * 诊疗项目自动补全
     * @param orgId
     * @param q
     * @param limit
     * @return
     */
    public List<ClinicItemDict> findListDict(String itemClass,String orgId,String q,String limit,String extId,String removeClass){
        String [] extIds=null;
		String [] itemClassList = null;
        String [] removeClasses = null;
		if(itemClass!=null){
			itemClassList= itemClass.split(",");
		}
        if(StringUtils.isNoneBlank(extId)){
            extIds= extId.split(",");
        }
        if(StringUtils.isNoneBlank(removeClass)){
            removeClasses= removeClass.split(",");
        }
        return clinicItemDictDao.findListDict(itemClassList,orgId,q,limit,extIds,removeClasses);
    }



}